package org.clipjoint.controller;

import org.clipjoint.entity.Publisher;
import org.clipjoint.service.PublisherService;
import org.clipjoint.service.impl.PublisherServiceImpl;
import org.clipjoint.vo.JsonResult;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/publisher")
public class PublisherController extends HttpServlet {

    private static final long serialVersionUID = 2L;

    private PublisherService publisherService = new PublisherServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String method = request.getParameter("method");

        switch (method) {
            case "showAll":
                showAll(request, response);
                break;
            default:
                break;
        }
    }

    private void showAll(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        // 调用service修改用户信息
        List<Publisher> publisherList = null;
        try {
            publisherList = publisherService.showAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        response.getWriter().print(JsonResult.builder().data(publisherList).code(0).build().toJsonString());

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
